package cm.util.init;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统初始化调用过滤器<br>
 * 需要进行初始化的模块需有一个实现cm.util.init.Init接口的类<br>
 * 并且把该类的名称配置到"initClassName"参数中
 * 
 * @author 苏玮
 *
 * 2010-12-2
 *
 */
public class SystemInitFilter implements Filter {
	
	private static Log log=LogFactory.getLog(SystemInitFilter.class);

	public void destroy() {
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		arg2.doFilter(arg0, arg1);
	}

	@SuppressWarnings("unchecked")
	public void init(FilterConfig arg0) throws ServletException {
		log.info("开始系统初始化！");
		String classNames=arg0.getInitParameter("initClassName");

		String[] classNameArray=classNames.split(",");
		for(String className:classNameArray){
			try{
				
				Class<Object> initClass=(Class<Object>)Class.forName(className.trim());
				Init init=(Init)initClass.newInstance();
				init.init(arg0.getServletContext());
			}catch(ClassNotFoundException e){
				log.error("找不到名为："+className+"的类！", e);
			}catch(Exception e){
				log.error("实例化初"+className+"出错！", e);
			}
			
		}
	}

}
